<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button v-has="30301" type="primary" size="small" @click="add">新增</el-button>
        </div>
        <el-table :data="table_body" border stripe :max-height="tableHeight">
          <el-table-column prop="clientName" label="供应商" />
          <el-table-column prop="contactWay" label="联系方式" />
          <el-table-column prop="companyEmail" label="公司邮箱" />
          <el-table-column prop="companyAddress" label="公司地址" />
          <el-table-column prop="status" label="状态" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="30301" size="small" shape="circle" type="text" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="openDrawer" :title="drawerTitle" width="60%">
      <el-form>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="供应商名称" label-position="top" required>
              <el-input v-model="formData.clientName" placeholder="供应商名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商联系方式" label-position="top" required>
              <el-input v-model="formData.contactWay" placeholder="供应商联系方式" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="联系邮箱" label-position="top" required>
              <el-input v-model="formData.companyEmail" placeholder="联系邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址" label-position="top" required>
              <el-input v-model="formData.companyAddress" placeholder="公司地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="状态" label-position="top" required>
              <el-select v-model="formData.enable" style="width:100%">
                <el-option value="true" label="启用" />
                <el-option value="false" label="停用" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-right: 8px" @click="openDrawer = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      openDrawer: false,
      table_body: [

      ],
      formDataDefault: {
        id: '',
        clientName: '',
        contactWay: '',
        companyEmail: '',
        companyAddress: '',
        status: 1,
        enable: 'true'
      },
      formData: {
        id: '',
        clientName: '',
        contactWay: '',
        companyEmail: '',
        companyAddress: '',
        status: 1,
        enable: 'true'
      },
      queryData: {
        pageSize: 50,
        currentPage: 0,
        enable: 'true'
      },
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      drawerTitle: '新增'
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 250
    },
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
    this.list()
  },
  mounted() {
  },
  methods: {
    list() {
      request({
        url: 'supplier/query?pageNum=' + this.queryData.currentPage + '&pageSize=' + this.queryData.pageSize,
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.table_body = res.data.records
            this.pageTotal = res.data.total
          }
        }
      })
    },
    submit() {
      if (this.formData.enable === 'true') {
        this.formData.status = 1
      } else {
        this.formData.status = 0
      }
      let url = 'supplier/add'
      if (this.formData.id) {
        url = 'supplier/update'
      }
      request({
        url: url,
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.openDrawer = false
          this.list()
        }
      })
    },
    edit(row) {
      this.drawerTitle = '编辑'
      copyProperty(this.formData, row)
      if (row.status === 1) {
        this.formData.enable = 'true'
      } else {
        this.formData.status = 'false'
      }
      this.openDrawer = true
    },
    add() {
      copyProperty(this.formData, this.formDataDefault)
      this.openDrawer = true
    },
    openDefDrawer() {
      this.openChargeDefDrawer = true
    },
    closeDefDrawer() {
      this.openChargeDefDrawer = false
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
